package yxy.game.pm2.active.command.tengacha;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actbeforehand;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_十连预抽;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActTenGachaInfoRequest;

@MessageType(proto = Command.ActTenGachaInfo, classtype = ActiveMessage.ActTenGachaInfoRequest.class)
public class CMD_Active_十连预抽_Info extends Command<ActTenGachaInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_十连预抽_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActTenGachaInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();

		ActiveMessage.ActTenGachaInfoResponse.Builder response = ActiveMessage.ActTenGachaInfoResponse.newBuilder();
		
		ActiveModule_十连预抽 am = ActiveModule_十连预抽.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		actbeforehand execl = ActDataCenter.getData(am.getId(), actbeforehand.class, pcode);
		if(execl == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		response.setGroup(am.getGroup());
		String taskcode = am.getIdcodemap().get(am.getId());
		
		Task taskbean = Task.get(pcode, taskcode);
		response.setTaskid(execl.getTaskid());
		response.setProgress(taskbean.getProgress());

 		response.addTgs(deal(1, am, execl, taskbean).build());
 		response.addTgs(deal(2, am, execl, taskbean).build());
 		response.addTgs(deal(3, am, execl, taskbean).build());
		
		send(messageSource, response, Result.Success);
	}

	private ActiveMessage.TenGachaInfo.Builder deal(int type, ActiveModule_十连预抽 am, actbeforehand execl, Task taskbean){
		ActiveMessage.TenGachaInfo.Builder build = ActiveMessage.TenGachaInfo.newBuilder();
		
		boolean display = am.getHerogroup().contains(type) ? true : false;
		
		int[] heroids = new int[]{};
		
		if(type == 1) {
			heroids = execl.getHeroid1();
		}
		if(type == 2) {
			heroids = execl.getHeroid2();
		}
		if(type == 3) {
			heroids = execl.getHeroid3();
		}
		
		build.setDisplay(display);
		for(int i = 0; i < heroids.length; i++) {
			build.addHeroids(heroids[i]);
		}
		build.setType(type);
		
		if(am.getGetherotype() != 0) {
			if(type == am.getGetherotype()) {
				build.setCanget(DrawStatus.hadget.getStatus());
			}else {
				build.setCanget(DrawStatus.cannotget.getStatus());
			}
		}else {
			if(!display) {
				build.setCanget(DrawStatus.nodisplay.getStatus());
			}else {
				if(taskbean.isReward()) {
					build.setCanget(DrawStatus.hadget.getStatus());
				}
				
				if(!taskbean.isFinish()) {
					build.setCanget(DrawStatus.cannotget.getStatus());
				}
				
				if(taskbean.isFinish() && !taskbean.isReward()) {
					build.setCanget(DrawStatus.canget.getStatus());
				}
			}
//			boolean can = display && taskbean.isFinish() && !taskbean.isReward();
//			int canget =  can ? ActiveModule_十连预抽.canget : ActiveModule_十连预抽.cannotget;
//			build.setCanget(canget);
		}
		
		return build;
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActTenGachaInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
